package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingProjectVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class ReservationServiceImpl implements ReservationService {
   @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public Integer getCancelledReservationCount() {
        Long user = UserThreadLocal.getUserId();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(now);
        LocalDateTime endTime = LocalDateTimeUtil.endOfDay(now);
        return reservationMapper.getCancelledReservationCount(user, startTime, endTime);
    }

    @Override
    public Map<String, Object> getReservationCountByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(localDateTime);
        LocalDateTime endTime = LocalDateTimeUtil.endOfDay(localDateTime);
        return reservationMapper.getReservationCountByTime(startTime,endTime);
    }

    @Override
    public void addReservation(Reservation reservation) {
        Integer count = reservationMapper.getReservationCount(reservation.getTime());
        if (count >= 6) {
            throw new BaseException(BasicEnum.RESERVATION_COUNT_UPPER_LIMIT);
        }
        Integer countMobile = reservationMapper.getReservationCountByMobile(reservation.getMobile(), reservation.getTime());
        if (countMobile != 0) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_FAIL);
        }
        reservation.setStatus(0);
        reservationMapper.addReservation(reservation);
    }

    @Override
    public PageResponse<ReservationVo> getByPage(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Reservation> reservation = reservationMapper.selectByPage(status);
        PageResponse<ReservationVo> reservationVoPageResponse = PageResponse.of(reservation, ReservationVo.class);
        return reservationVoPageResponse;
    }

    @Override
    public void cancelReservation(Long id) {
        String status = reservationMapper.getReservationStatus(id);
        if (!"0".equals(status)) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_GET_STATUS);
        }
        reservationMapper.cancelReservation(id);
    }


//    @Scheduled(cron = "0 * * * * ?")
    public void cancelReservation() {
        LocalDateTime time = LocalDateTime.now();
        reservationMapper.cancelReservationAuto(time);
    }
}
